package com.powermgr.boringtest.task.impl;

import com.powermgr.boringtest.bean.PrData;
import com.powermgr.boringtest.task.PrTask;
import com.powermgr.boringtest.utils.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

import static com.powermgr.boringtest.utils.ConfigUtils.SN;
import static com.powermgr.boringtest.utils.ConfigUtils.SUCCESS;

/**
 * RunTestCasesTask
 *
 * @author qiuxiangdong
 * @date 2023/9/23 10:26
 */
@Log4j2
public class RunTestCasesPrTask implements PrTask<PrData> {
    @Override
    public boolean execute(PrData prData) {
        runCase(prData.getType(), prData.getModule(), prData.getSn_id());
        prData.setState("2");
        EmailUtils.SendEmail(prData, SUCCESS);
        prData.setState("3");
        return true;
    }
    private void runCase(List<String> prType, List<String> prModule, String sn){
        log.info("*启动测试框架");
        int count = 0;
        for (String module : prModule) {
            for (String type : prType) {
                FileZipUtils.deleteFile(SN + File.separator + sn +  File.separator + "case" +  File.separator + "acts" + File.separator + "testcases");
                FileZipUtils.createFile(SN + File.separator + sn +  File.separator + "case" +  File.separator + "acts" + File.separator + "testcases");
                FileZipUtils.deleteFile(SN + File.separator + sn +  File.separator + "case" +  File.separator + "hats" + File.separator + "testcases");
                FileZipUtils.createFile(SN + File.separator + sn +  File.separator + "case" +  File.separator + "hats" + File.separator + "testcases");
                FileZipUtils.deleteFile(SN + File.separator + sn +  File.separator + "case" +  File.separator + "dcts" + File.separator + "testcases");
                FileZipUtils.createFile(SN + File.separator + sn +  File.separator + "case" +  File.separator + "dcts" + File.separator + "testcases");
                FileZipUtils.deleteFile(SN + File.separator + sn +  File.separator + "case" +  File.separator + "tests");
                FileZipUtils.createFile(SN + File.separator + sn +  File.separator + "case" +  File.separator + "tests");
                runTest(type, module, sn);
                if (count != 0){
                    reboot(sn);
                }
                count ++;
            }
        }
        log.info("*测试用例执行完成");
    }
    private void runTest(String prType, String prModule, String sn){
        List<String> caseList = InitUtils.getCaseValue(prType, prModule);
        if (TypeUtils.IsXTS(prType)){
            if (caseList != null){
                String names = String.join(";", caseList) + ";";
                copyTempXTS(prType, sn);
                CmdUtils.runXTS(prType, names, sn);
            }
        }
        if (TypeUtils.IsTDD(prType)){
            if (caseList != null){
                if (caseList.size() > 1){
                    for (String name : caseList){
                        copyTempTDD(prType, name, sn);
                    }
                    CmdUtils.runTDD(prType,caseList.get(0).split(" ")[0], sn);
                } else {
                    for (String name : caseList){
                        copyTempTDD(prType, name, sn);
                        CmdUtils.runTDD(prType, name, sn);
                    }
                }
            }
        }
    }

    private void reboot(String sn){
        CmdUtils.runCmd("hdc -t " + sn + " shell reboot");
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        CmdUtils.runCmd("hdc -t " + sn + " shell power-shell setmode 602");
        CmdUtils.runCmd("hdc -t " + sn + " shell shell hilog -Q pidoff");
        CmdUtils.runCmd("hdc -t " + sn + " shell hilog -b D");
    }

    private static void copyTempTDD(String type, String module, String sn) {
        if (TypeUtils.IsUT(type)) {
            copyTempUT(module, sn);
        }
        if (TypeUtils.IsFUZZ(type)) {
            copyTempFUZZ(module, sn);
        }
        if (TypeUtils.IsST(type)) {
            copyTempST(module, sn);
        }
        if (TypeUtils.IsBenchmark(type)) {
            copyTempBenchmark(module, sn);
        }
        if (TypeUtils.IsMST(type)) {
            copyTempMST(module, sn);
        }
    }
    private  static void copyTempXTS(String type, String sn) {
        if (TypeUtils.IsActs(type)) {
            copyTempACTS(sn);
        }
        if (TypeUtils.IsHats(type)) {
            copyTempHATS(sn);
        }
        if (TypeUtils.IsDcts(type)) {
            copyTempDCTS(sn);
        }
    }
    private static void copyTempACTS(String sn) {
        try {
            File file = new File(SN + File.separator + sn +  File.separator + "pr" + File.separator + "xts"
                    + File.separator + "suites" + File.separator + "acts" + File.separator + "testcases");
            if (file.exists()){
                FileUtils.copyDirectory(file,
                        new File(SN + File.separator + sn +  File.separator + "case" +  File.separator
                                + "acts" + File.separator + "testcases"));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void copyTempHATS(String sn) {
        try {
            File file = new File(SN + File.separator + sn +  File.separator + "pr" + File.separator + "xts"
                    + File.separator + "suites" + File.separator + "hats" + File.separator + "testcases");
            if (file.exists()){
                FileUtils.copyDirectory(file,
                        new File(SN + File.separator + sn +  File.separator + "case" +  File.separator
                                + "hats" + File.separator + "testcases"));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void copyTempDCTS(String sn) {
        try {
            File file = new File(SN + File.separator + sn +  File.separator + "pr" + File.separator + "xts"
                    + File.separator + "suites" + File.separator + "dcts" + File.separator + "testcases");
            if (file.exists()){
                FileUtils.copyDirectory(file,
                        new File(SN + File.separator + sn +  File.separator + "case" +  File.separator
                                + "dcts" + File.separator + "testcases"));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void copyTempUT(String prModule, String sn) {
        if (prModule.contains("*")){
            prModule = prModule.replace("*", File.separator);
        }
     
        log.info("copyTempUT: " + prModule);
        try {
            File sourceFile = new File(SN + File.separator + sn +  File.separator + "pr"
                    + File.separator + "tdd" + File.separator + "tests" + File.separator + "unittest"
                    + File.separator +  prModule);
            File targetFile = new File(SN + File.separator + sn +  File.separator + "case"
                    +  File.separator + "tests" + File.separator + "unittest"
                    + File.separator +  prModule);
            if (sourceFile.isFile()) {
                FileUtils.copyFile(sourceFile, targetFile);
            } else if (sourceFile.isDirectory()) {
                FileUtils.copyDirectory(sourceFile, targetFile);
            } else {
                System.out.println("文件或目录不存在");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void copyTempMST(String prModule, String sn) {
        if (prModule.contains("*")){
            prModule = prModule.replace("*", File.separator);
        }
        log.info("copyTempMT: " + prModule);
        try {
            File sourceFile = new File(SN + File.separator + sn +  File.separator + "pr"
                    + File.separator + "tdd" + File.separator + "tests" + File.separator + "moduletest"
                    + File.separator +  prModule);
            File targetFile = new File(SN + File.separator + sn +  File.separator + "case"
                    +  File.separator + "tests" + File.separator + "moduletest"
                    + File.separator +  prModule);
            if (sourceFile.isFile()) {
                FileUtils.copyFile(sourceFile, targetFile);
            } else if (sourceFile.isDirectory()) {
                FileUtils.copyDirectory(sourceFile, targetFile);
            } else {
                System.out.println("文件或目录不存在");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void copyTempFUZZ(String prModule, String sn) {
        if (prModule.contains("*")){
            prModule = prModule.replace("*", File.separator);
        }
        log.info("copyTempFUZZ: " + prModule);
        try {
            File sourceFile = new File(SN + File.separator + sn +  File.separator + "pr"
                    + File.separator + "tdd" + File.separator + "tests" + File.separator + "fuzztest"
                    + File.separator +  prModule);
            File targetFile = new  File(SN + File.separator + sn +  File.separator + "case"
                    +  File.separator + "tests" + File.separator + "fuzztest"
                    + File.separator +  prModule);
            if (sourceFile.isFile()) {
                FileUtils.copyFile(sourceFile, targetFile);
            } else if (sourceFile.isDirectory()) {
                FileUtils.copyDirectory(sourceFile, targetFile);
            } else {
                System.out.println("文件或目录不存在");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        File resFile = new File(SN + File.separator + sn +  File.separator + "pr"
                + File.separator + "tdd" + File.separator + "tests" + File.separator + "res");
        if (resFile.exists()){
            try {
                FileUtils.copyDirectory(resFile,
                        new File(SN + File.separator + sn +  File.separator + "case"
                                +  File.separator + "tests" + File.separator + "res"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    private static void copyTempST(String prModule, String sn) {
        if (prModule.contains("*")){
            prModule = prModule.replace("*", File.separator);
        }
        log.info("copyTempST: " + prModule);
        try {
            File sourceFile = new File(SN + File.separator + sn +  File.separator + "pr"
                    + File.separator + "tdd" + File.separator + "tests" + File.separator + "systemtest"
                    + File.separator +  prModule);
            File targetFile = new File(SN + File.separator + sn +  File.separator + "case"
                    +  File.separator + "tests" + File.separator + "systemtest"
                    + File.separator +  prModule);
            if (sourceFile.isFile()) {
                FileUtils.copyFile(sourceFile, targetFile);
            } else if (sourceFile.isDirectory()) {
                FileUtils.copyDirectory(sourceFile, targetFile);
            } else {
                System.out.println("文件或目录不存在");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void copyTempBenchmark(String prModule, String sn) {
        if (prModule.contains("*")){
            prModule = prModule.replace("*", File.separator);
        }
        log.info("copyTempBenchmark: " + prModule);
        try {
            File sourceFile = new File(SN + File.separator + sn +  File.separator + "pr"
                    + File.separator + "tdd" + File.separator + "tests" + File.separator + "benchmark"
                    + File.separator +  prModule);
            File targetFile = new File(SN + File.separator + sn +  File.separator + "case"
                    +  File.separator + "tests" + File.separator + "benchmark"
                    + File.separator +  prModule);
            if (sourceFile.isFile()) {
                FileUtils.copyFile(sourceFile, targetFile);
            } else if (sourceFile.isDirectory()) {
                FileUtils.copyDirectory(sourceFile, targetFile);
            } else {
                System.out.println("文件或目录不存在");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}